#cmf. 13 feb 2013.
#whatever occurs to me for DYNaming....

#note: one of the 12mo subs had typo for 'ball', fixed in the master csv file but not original rtf
##############################################################################################
#clear workspace and load some packages
rm(list=ls())
require(lattice)
require(doBy)
require(reshape)

##############################################################################################
#read in master datafile
data = read.csv('DYNaming_AllData.csv')

##############################################################################################
#figure out total number of utterances for each subject
TotalUtterances = summaryBy(InstanceNum ~ SubjectID, data = data, FUN = function(x) { max(x) } )
colnames(TotalUtterances) = c('SubjectID', 'NumUtterances_Total')

#add AgeGroup column to NumUtterances.  dumb, HARDCODED
#fyi, has to be character for lattice plot to print the group values
AgeGroup = c('04','04','04','04','08','08','08','08','12','12','12','12','16','16','16','16','20','20','20','20')
TotalUtterances = cbind(AgeGroup, TotalUtterances)
rm(AgeGroup)

#plot histograms of total Num Utterances per age group
histogram(~TotalUtterances$NumUtterances_Total | TotalUtterances$AgeGroup, type = 'c', layout= c(1,5), col = 'green4', xlab = 'Total Number Of Utterances')

#get median, min and max number utterances per age group
MediansRanges = summaryBy(NumUtterances_Total ~ AgeGroup, data = TotalUtterances, FUN = function(x) { c(median = median(x), range = range(x)) } )
colnames(MediansRanges) = c('AgeGroup', 'Median_NumUtterances', 'Min_NumUtterances', 'Max_NumUtterances')

##############################################################################################
#i wanna know the number of Familiar & number of Novel utterances per subject

#change factors to characters in data (stupid datatype issue)
data$Name = as.character(data$Name)
data$Timestamp = as.character(data$Timestamp)
data$Familiarity = as.character(data$Familiarity)

Familiars = summaryBy(Familiarity ~ SubjectID, data = data, FUN = function(x) { sum(x == 'Familiar') } )
colnames(Familiars) = c('SubjectID', 'NumUtterances_Familiar')

Novels = summaryBy(Familiarity ~ SubjectID, data = data, FUN = function(x) { sum(x == 'Novel') } )
colnames(Novels) = c('SubjectID', 'NumUtterances_Novel')

#merge all utterance info dataframes 
NumUtteranceInfo = merge_recurse(list(TotalUtterances, Familiars, Novels))
rm(Familiars); rm(Novels); rm(TotalUtterances)

#i wanna know proportion of utterances that were Familiar
PropFamiliar = NumUtteranceInfo$NumUtterances_Familiar/NumUtteranceInfo$NumUtterances_Total
NumUtteranceInfo = cbind(NumUtteranceInfo, PropFamiliar)
rm(PropFamiliar)

#plot histos of propFamiliar utterances, by age group
histogram(~NumUtteranceInfo$PropFamiliar | NumUtteranceInfo$AgeGroup, type = 'c', layout= c(1,5), col = 'purple4', xlab = 'Prop Familiar Utterances')
hist(NumUtteranceInfo$PropFamiliar, xlab = "Proportion Familiar Utterances", main = 'All ages, Prop Familiar Utterances') #overall, not split by age

#relationship between total num utterances & prop Familiar?  ...not really...
plot(NumUtteranceInfo$NumUtterances_Total, NumUtteranceInfo$PropFamiliar, xlab = "Total Number Of Utterances", ylab = "Proportion Familiar Utterances")
xyplot(NumUtteranceInfo$PropFamiliar ~ NumUtteranceInfo$NumUtterances_Total | NumUtteranceInfo$AgeGroup, layout= c(1,5), xlab = 'Total Number Of Utterances', ylab = 'Proportion Familiar Utterances')


#stopped here for now....
#write.csv(MediansRanges, 'MediansRanges.csv', row.names = FALSE)
#write.csv(NumUtteranceInfo, 'NumUtteranceInfo.csv', row.names = FALSE)

########################################################################################################################
#want to know number of unique familiar & unique novel words, for each subject
#(didn't yield anything interesting or problematic)

UniqueWords = summaryBy(Name ~ SubjectID + Familiarity, data = data, FUN = function(x) { length(unique(x)) } )
colnames(UniqueWords) = c('SubjectID', 'Familiarity', 'NumUniqueWords')

#some reshaping. pull out Familiars & Uniques, merge to put the side-by-side. rancid code. gotta be a smoother way...
UniqueFamiliars = UniqueWords[UniqueWords$Familiarity == 'Familiar',]
UniqueFamiliars = UniqueFamiliars[,-2]
colnames(UniqueFamiliars) = c('SubjectID', 'Unique_Familiars')
UniqueNovels = UniqueWords[UniqueWords$Familiarity == 'Novel',]
UniqueNovels = UniqueNovels[,-2]
colnames(UniqueNovels) = c('SubjectID', 'Unique_Novels')
Uniques = merge_recurse(list(UniqueFamiliars, UniqueNovels))
TotalWordTypes = Uniques$Unique_Familiars + Uniques$Unique_Novels
Uniques = cbind(Uniques,TotalWordTypes)

#what proportion of all unique words are Familiar
PropFamiliarWordTypes = Uniques$Unique_Familiars/Uniques$TotalWordTypes
Uniques = cbind(Uniques, PropFamiliarWordTypes)

#plot prop familiar word types, histo for whole sample
hist(Uniques$PropFamiliarWordTypes, ylim = c(0,12), xlim = c(0,1), xlab = "Of all unique word types, what prop Familiar", main = "Word Types: Proportion Familiar")

#combine all by-subject info into one dataframe
All = merge_recurse(list(NumUtteranceInfo, Uniques))

#clear some variables
rm(UniqueFamiliars); rm(UniqueNovels); rm(UniqueWords); rm(PropFamiliarWordTypes); rm(TotalWordTypes); rm(NumUtteranceInfo); rm(Uniques)

########################################################################################################################
#first stab at thinking about repeated names. probably some bad thinking and bad coding in here....drafty....

#copy the Name column and shift it down 1
NextName = data$Name
NextName = c(NextName[-1], 'LAST') #delete the first row, make last value say "LAST"
data = cbind(data, NextName)
rm(NextName)

#replace the very last instance of each subject with "LAST"
data$NextName[which(data$InstanceNum == 1)-1] = "LAST"   #not optimal code, cuz depends on dataframe being in original order. should code this better...

#if the name repeats, put a 1 in RepeatName column
#name repeats if the name in Name and NextName match
RepeatName = rep(NA, dim(data)[1])
data = cbind(data, RepeatName)
rm(RepeatName)
data$RepeatName[data$Name == data$NextName] = 1
data$RepeatName[data$Name != data$NextName] = 0

#################################################################################################################
#ok, for all analyses of repetitions, gonna delete the rows with 'LAST' (dunno if this is correct analysis decision)
#so, make a new dataframe, with each subject's "LAST" row removed
RepData = data
LASTRows = which(RepData$NextName == 'LAST')
RepData = RepData[-LASTRows,]
rm(LASTRows)

#summarize by subject, how many repeated names
RepeatedNames = summaryBy(RepeatName ~ SubjectID, data = RepData, FUN = function(x) { sum(x) } )
colnames(RepeatedNames) = c('SubjectID', 'RepeatedNames_Total')

#need to know how many rows per subject
RepDataRows = summaryBy(RepeatName ~ SubjectID, data = RepData, FUN = function(x) { length(x) } )
colnames(RepDataRows) = c('SubjectID', 'NumRows_RepeatAnalysisTotal')

#now i wanna know repeated names just of familiars
FamiliarSubset = subset(RepData, RepData$Familiarity == 'Familiar')
RepeatedNamesFamiliars = summaryBy(RepeatName ~ SubjectID, data = FamiliarSubset, FUN = function(x) { sum(x) } )
colnames(RepeatedNamesFamiliars) = c('SubjectID', 'RepeatedNames_Familiar')

#need to know numRows for just familiars
RepDataRowsFamiliar = summaryBy(RepeatName ~ SubjectID, data = FamiliarSubset, FUN = function(x) { length(x) } )
colnames(RepDataRowsFamiliar) = c('SubjectID', 'NumRowsFamiliar_RepeatAnalysisTotal')

#now i wanna know repeated names just of novels
NovelSubset = subset(RepData, RepData$Familiarity == 'Novel')
RepeatedNamesNovels = summaryBy(RepeatName ~ SubjectID, data = NovelSubset, FUN = function(x) { sum(x) } )
colnames(RepeatedNamesNovels) = c('SubjectID', 'RepeatedNames_Novel')

#need to know numRows for just novels
RepDataRowsNovel = summaryBy(RepeatName ~ SubjectID, data = NovelSubset, FUN = function(x) { length(x) } )
colnames(RepDataRowsNovel) = c('SubjectID', 'NumRowsNovel_RepeatAnalysisTotal')

#merge!
All = merge_recurse(list(All,RepDataRows,RepDataRowsFamiliar, RepDataRowsNovel, RepeatedNames, RepeatedNamesFamiliars, RepeatedNamesNovels))

#clear some variables
rm(FamiliarSubset); rm(NovelSubset); rm(RepDataRows); rm(RepeatedNames); rm(RepeatedNamesFamiliars); rm(RepeatedNamesNovels); rm(RepDataRowsFamiliar); rm(RepDataRowsNovel)

###########################################################################################################
#Prop repeated names. 
#note, denominator does not include the last token for each subject. maybe it should... fix later....

#compute some proportions
#overall
PropRepeated = All$RepeatedNames_Total/All$NumRows_RepeatAnalysisTotal
PropRepeatedFamiliar = All$RepeatedNames_Familiar/All$NumRowsFamiliar_RepeatAnalysisTotal
PropRepeatedNovel = All$RepeatedNames_Novel/All$NumRowsNovel_RepeatAnalysisTotal
All = cbind(All, PropRepeated, PropRepeatedFamiliar, PropRepeatedNovel)

#histos: whole sample
hist(All$PropRepeated, main = 'Proportion of ALL tokens that were repeated (whole sample)', xlab = 'Prop Repeated Tokens', xlim = c(0,1), col = 'firebrick3' )
hist(All$PropRepeatedFamiliar, main = 'Proportion of FAMILIAR tokens that were repeated (whole sample)', xlab = 'Prop Repeated Familiar Tokens', xlim = c(0,1), col = 'purple2' )
hist(All$PropRepeatedNovel, main = 'Proportion of NOVEL tokens that were repeated (whole sample)', xlab = 'Prop Repeated Novel Tokens', xlim = c(0,1), col = 'blue' )

#histos: by age group
histogram(~All$PropRepeated | All$AgeGroup, type = 'c', layout = c(1,5), xlab = 'Proportion of tokens that were repeated', main = "Repeated ALL Tokens, By Age Group", col = 'firebrick3')
histogram(~All$PropRepeatedFamiliar | All$AgeGroup, type = 'c', layout = c(1,5), xlab = 'Proportion of familiar tokens that were repeated', main = "Repeated FAMILIAR Tokens, By Age Group", col = 'purple2')
histogram(~All$PropRepeatedNovel | All$AgeGroup, type = 'c', layout = c(1,5), xlab = 'Proportion of novel tokens that were repeated', main = "Repeated NOVEL Tokens, By Age Group", col = 'blue')

#clear some variables
rm(PropRepeated); rm(PropRepeatedFamiliar); rm(PropRepeatedNovel)

#stopped here for now
write.csv(All, 'BySubjectInfo.csv', row.names = FALSE)